<?php
/*
 * @Author: Wulala
 * @Date: 2025-04-08 11:46:09
 * @LastEditTime: 2025-04-11 16:15:06
 * @LastEditors: Wulala
 * @Description: 
 * @FilePath: \exchange\app\api\service\SignService.php
 * 可以输入预定的版权声明、个性签名、空行等
 */

namespace app\api\service\delivery\shansong;


class ApiService
{
    // 基础参数
    protected $config;//配置

    // 请求地址
    protected $openCitiesLists = '/openapi/merchants/v5/openCitiesLists';       //1.查询开通城市
    protected $queryAllStores = '/openapi/merchants/v5/queryAllStores';         //2.分页查询商户店铺
    protected $optionalTravelWay = '/openapi/merchants/v5/optionalTravelWay';   //4.查询城市可指定的交通工具
    protected $orderCalculate = '/openapi/merchants/v5/orderCalculate';         //5.订单计费
    protected $orderPlace = '/openapi/merchants/v5/orderPlace';                 //6.提交订单
    protected $addition = '/openapi/merchants/v5/addition';                     //7.订单加价
    protected $orderInfo = '/openapi/merchants/v5/orderInfo';                   //8.查询订单详情
    protected $courierInfo = '/openapi/merchants/v5/courierInfo';               //9.查询闪送员位置信息
    protected $calculateOrderAddWeightFee = '/openapi/merchants/v5/calculateOrderAddWeightFee'; //10.查询订单续重加价金额
    protected $payAddWeightFee = '/openapi/merchants/v5/payAddWeightFee';       //11.支付订单续重费用
    protected $preAbortOrder = '/openapi/merchants/v5/preAbortOrder';           //12.订单预取消
    protected $abortOrder = '/openapi/merchants/v5/abortOrder';                 //13.订单取消
    protected $confirmGoodsReturn = '/openapi/merchants/v5/confirmGoodsReturn'; //14.确认物品送回
    protected $storeOperation = '/openapi/merchants/v5/storeOperation';         //15.新增店铺，16.修改店铺
    protected $getUserAccount = '/openapi/merchants/v5/getUserAccount';         //17.查询账号额度
    protected $updateToMobile = '/openapi/merchants/v5/updateToMobile';         //19.修改收件人手机号
    protected $addStores = '/openapi/merchants/v5/addStores';                   //20.批量新增店铺
    protected $orderEta = '/openapi/merchants/v5/orderEta';                     //21.查询订单ETA
    protected $appendOrder = '/openapi/merchants/v5/appendOrder';               //22.订单追单
    protected $qualityDeliverySwitch = '/openapi/merchants/v5/qualityDeliverySwitch';//23.查询是否支持尊享送
    protected $qualityDeliveryStatus = '/openapi/merchants/v5/qualityDeliveryStatus';//24.查询尊享送达成状态


    /*
    * $config: 配置信息
    */
    public function __construct(array $config)
    {
        $this->config = $config;
    }


    // 闪送平台---统一请求接口
    public function  sendUnite($urlApi,$data=array())
    {
        $url = $this->config['url'].$this->$urlApi;
    
        $param['App_secret'] =  $this->config['App_secret'];
        $param['clientId'] = $this->config['clientId'];
        $param['shopId'] = $this->config['shopId'];
        $param['timestamp'] = time().'000';

        $param['sign'] = $this->generateShanSign($param,$data);
        if (!empty($data)){
            $param['data'] = json_encode($data);
        }

        unset($param['App_secret']);
        $param = array_filter($param);
        $res = curl_post($url, $param);
        $res_d = json_decode($res,true);
        return $res_d;
    }

    // 闪送平台---统一返回接口
    public function  returnUnite($res=array())
    {
        if(isset($res['status'])&&$res['status']==200){
           return result_success('success',$res["data"]);
        }else{
           return result_error($res["msg"]??'请求失败');
        } 
    }


    // 计算签名
    public function generateShanSign($param, $data=[])
    {
        $sign = $param['App_secret']."clientId".$param['clientId']."shopId".$param['shopId']."timestamp".$param['timestamp'];
        if (!empty($data) && is_array($data)){
            $sign = $param['App_secret']."clientId".$param['clientId']."data".json_encode($data)."shopId".$param['shopId']."timestamp".$param['timestamp'];
        }
        $sign = strtoupper(md5($sign));

        return $sign;
    }

}